package com.xhkj.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xhkj.mapper.my.ChangDiMapper;
import com.xhkj.model.ChangDi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "01.场地" })
@RestController
@RequestMapping("changdi")
public class ChangDiController {

	@Autowired
	private ChangDiMapper mapper;

	@ApiOperation(value = "增加场地")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "req", value = "场地实体", required = true, dataTypeClass = ChangDi.class, paramType = "body"),
	})
	@PostMapping("add")
	public Object add(@RequestBody ChangDi req) {
		if(req.getId() == null) {
			req.setCjsj(LocalDateTime.now());
			mapper.insert(req);
		}else {
			mapper.updateByPrimaryKey(req);
		}
		return req;
	}
	@ApiOperation(value = "修改场地")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "req", value = "场地实体", required = true, dataTypeClass = ChangDi.class, paramType = "body"),
	})
	@PostMapping("update")
	public Object update(@RequestBody ChangDi req) {
		mapper.updateByPrimaryKey(req);
		return req;
	}
	
	@PostMapping("getAll")
	public Object getAll(
			@RequestBody ChangDi req) {
		List<ChangDi> select = mapper.selectMy(req);
		return select;
	}
	@PostMapping("getKey")
	public Object getKey(HttpServletRequest request) {
		Object key = request.getSession().getAttribute("sessionid");
		String sessionid = UUID.randomUUID().toString();
		if(key != null) {
			sessionid = key.toString();
		}else {
			request.getSession().setAttribute("sessionid",sessionid);
		}
		System.out.println(sessionid);
		return sessionid;
		
	}


}
